export default class TreeAction {
    constructor(comp) {
        this.comp = comp;
    }

    getTreeData = () => {
        const {props} = this.comp;
        const {dispatch, main} = props;
        const {orgValue, language} = main;
        if (!language['gx6008-000287']) return;
        if (!orgValue || !orgValue.refpk) {
            this.setTreeData([], true);
            return;
        }
        dispatch({
            type: 'main/getConfig',
            payload: {
                pk_org: orgValue.refpk,
                infoset_code: 'bd_psndoc',
                isqrypsndoc: 'Y'
            }
        })
            .then(async (res) => {
                await props.dispatch({
                    type: 'main/update',
                    payload: {
                        approveMap: res.data.approveMap,
                        disEditMap: res.data.disEditMap,
                        gridMap: res.data.gridMap,
                        infosetArray: res.data.infosetArray
                    }
                });
                this.setTreeData(res.data);
            })
    };

    setTreeData = async (data = {}, isInit) => {
        const {props, action} = this.comp;
        const {syncTree, main} = props;
        const {language} = main;
        const {infosetArray = []} = data;
        const children = this.formatTree(infosetArray);
        let root = [{
            children,
            code: language['gx6008-000287'],
            id: null,
            isOrgStruSort: false,
            innercode: null,
            isleaf: false,
            key: 'root',
            name: language['gx6008-000287'],
            nodeData: {
                nodeValue: {}
            },
            pid: null,
            refname: language['gx6008-000287'],
            refpk: 'root',
            title: language['gx6008-000287']
        }];
        syncTree.setSyncTreeData('authorList', root);
        if (isInit) return;
        const selectKey = children[0].refpk;
        syncTree.setNodeSelected('authorList', selectKey);
        syncTree.openNodeByPk('authorList', ['root']);
        await props.dispatch({
            type: 'main/update',
            payload: {
                selectKey,
                handleItem: children[0].nodeData.nodeValue
            }
        });
        action.tableAct.getTableData(children[0].nodeData.nodeValue.infosetcode);
    };

    formatTree = (data = []) => {
        return data.map(item => {
                return {
                    children: [],
                    code: item.pk_infoset,
                    id: null,
                    isOrgStruSort: false,
                    innercode: null,
                    isleaf: true,
                    key: item.pk_infoset,
                    name: item.infosetname,
                    nodeData: {
                        nodeValue: item
                    },
                    pid: null,
                    refname: item.infosetname,
                    refpk: item.pk_infoset,
                    title: item.infosetname
                }
            }
        )
    };

    onTreeSelect = async (pk, value) => {
        const {props, action} = this.comp;
        const {main: {selectKey}, syncTree} = props;
        if (pk === 'root') {
            syncTree.setNodeSelected('authorList', selectKey);
            return;
        }
        await props.dispatch({
            type: 'main/update',
            payload: {
                selectKey: pk,
                handleItem: value.nodeData.nodeValue
            }
        });
        action.tableAct.getTableData(value.nodeData.nodeValue.infosetcode)
    };
}